package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Company;
import com.blb.entity.User;
import com.blb.service.ICompanyService;
import com.blb.service.IUserService;
import com.blb.utils.Response;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class RegisterController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyService companyService;

    @ResponseBody
    @PostMapping("/register")
    public Response register(Company company, User user) {

        System.out.println("111111111111111111111111111111111");

        //设置公司id
        String cid = UUIDUtils.getId().substring(0, 4).toUpperCase();
        company.setId(cid);
        company.setCompanyStatus("已启用");
        company.setCompanyRole("代理商");
        company.setCompanyId(cid);
        company.setCompanyRegisterDate(new Date());
        //传入到用户里面
        user.setId(UUIDUtils.getId());
        user.setCompanyId(company.getId());
        ShiroUtils.getPass(user);
        userService.userRegiest(company, user);
        return Response.success();
    }

    @ResponseBody
    @PostMapping("/registExist")
    public Response registerisExist(String companyName) {
        Company company = companyService.selectByCompanyName(companyName);
        if (company == null) {
            return Response.success();
        } else {
            return Response.error("企业名称已存在");
        }
    }

    @ResponseBody
    @PostMapping("/registUser")
    public Response registUser(String useraccount) {
        User user = userService.getOne(new QueryWrapper<User>().eq("user_account", useraccount));
        if (user == null) {
            return Response.success();
        } else {
            return Response.error("企业账号已存在");
        }
    }

    @ResponseBody
    @PostMapping("/registEmail")
    public Response registEmail(String companyEmail) {
        Company company = companyService.getOne(new QueryWrapper<Company>().eq("company_email", companyEmail));
        if (company == null) {
            return Response.success();
        } else {
            return Response.error("邮箱已存在");
        }
    }

}
